var ds_role_select = new Ext.data.Store({
	url : '/supervise/sys/role/findAllRole.action',
	reader : new Ext.data.JsonReader({
				root : 'root'
			}, [{
						name : 'id',
						type : 'int'
					}, {
						name : 'name',
						type : 'string'
					}])
});
		
var combo_select_role = new Ext.form.ComboBox({
			xtype : 'combo',
			fieldLabel : '<span style="color: red">*&nbsp;</span>角色',
			hiddenName : 'user.orgId',
			valueField : 'id',
			displayField : 'name',
			mode : 'remote',
			store : ds_role_select,
			selectOnFocus : true,
			editable : false,
			allowBlank : false,
			width: 200,
			typeAhead: true,
			readOnly: true,
			triggerAction : 'all',
			loadingText : '加载中...',
			emptyText : '请选择角色',
			listeners : {
				'select' : function(combo, record, index) {
					roleId = combo.getValue();
					grid_rolepermission.getSelectionModel().clearSelections();
					ds_rolepermission.baseParams.roleId = roleId;
					ds_rolepermission.load();
//					grid_rolepermission.render();
				}
			}});

var btn_save_rolepermission = new Ext.Button({
	text : '保存',
	iconCls : 'icon-edit',
	handler : function() {
		var records = grid_rolepermission.getSelectionModel().getSelections();
		var isNotRecords = (records == null || records.length  == 0);
		  var ids = [];
            for(var i=0; i<records.length; i++){
                var pId = records[i].get('id');  
                ids.push(pId);                //向数组后添加元素
        }
		Ext.Msg.confirm('确认修改', isNotRecords ? '确认取消所有的权限?' : '确认要修改角色的权限?', function(btn) {
			if (btn == 'yes') {
				Ext.Ajax.request({
							url : '/supervise/sys/rolepermission/saveRolePermission.action',
							params : {
								ids : ids,
								roleId: combo_select_role.getValue()
							},
							success : function() {
								Ext.Msg.show({
											title : '提示',
											msg : '成功修改角色权限!',
											buttons : Ext.Msg.OK,
											icon : Ext.Msg.OK
										});
							},
							failure : function() {
								Ext.Msg.show({
											title : '错误提示',
											msg : '修改时发生错误!',
											buttons : Ext.Msg.OK,
											icon : Ext.Msg.ERROR
										});
							}
						});
			}
		});
	}
});
var cm_rolepermission = new Ext.grid.ColumnModel([
	new Ext.grid.CheckboxSelectionModel(),
	new Ext.grid.RowNumberer({header : '序号', width : 40, dataIndex: 'id'}),
	{id: 'resName',header: "权限点名称", width: 100, sortable: true, dataIndex: 'resName'},
    {id: 'resUrl',header: "访问 URL", width: 200, sortable: true, dataIndex: 'resUrl'},
    {id: 'resId',header: "权限引用资源ID", width: 80, sortable: true, dataIndex: 'resId'},
    {id: 'resType',header: "权限类型", width: 80, sortable: true, dataIndex: 'resType'},
    {id: 'remark', header: "备注", width: 300, sortable: true, dataIndex: 'remark'},
    {id: 'checked',header: "是否有权限", width: 10, hidden : true, dataIndex: 'checked',renderer: function(value, meta, rcd, rIndex, cIndex, st) {if(value){var model = grid_rolepermission.getSelectionModel();model.selectRow(rIndex, true);}}}
]);
  
var ds_rolepermission = new Ext.data.Store({
	proxy : new Ext.data.HttpProxy({
		url : '/supervise/sys/rolepermission/findAllRolePermission.action'
	}),
//	baseParams: {roleId: roleId},
//	autoLoad: true,
	reader : new Ext.data.JsonReader({
		root : 'root',
		successProperty :'success'
	}, [{
		name : 'id',
		type : 'int'
	}, {
		name : 'resId',
		type : 'string'
	}, {
		name : 'resName',
		type : 'string'
	},{
		name : 'resType',
		type : 'string'
	},{
		name : 'resUrl',
		type : 'string'
	},{
		name : 'remark',
		type : 'string'
	},{
		name : 'checked',
		type : 'boolean'
	}])

});
 
var grid_rolepermission = new Ext.grid.GridPanel({
	title : '角色权限管理',
	iconCls : 'icon-grid',
	loadMask : {
		msg : '数据加载中...'
	},
	region : 'center',
	cm : cm_rolepermission,
	ds : ds_rolepermission,
	sm : new Ext.grid.CheckboxSelectionModel({
		singleSelect : false
	}),
	enableColumnMove : false,
	trackMouseOver : false,
	frame : true,
	clicksToEdit : 1,
	tbar : ['<span style="color: red">*&nbsp;</span>角色 ',combo_select_role, '-', '&nbsp;&nbsp;&nbsp;',
		btn_save_rolepermission],
	listeners : {
				'render' : function(e) {
					ds_permission.load();
				}
			}
});

 		
var p_role_permission = {
	id : 'rolepermission-panel',
	border : false,
	layout : 'border',
	items : [grid_rolepermission]
};